import axios from './ajax'
import type { ResDataType } from './ajax'

type SearchOption = {
  keyword: string
  isStar: boolean
  isDeleted: boolean
  pageSize: number
  page: number
}

export function sleep(time = 1000) {
  return new Promise((resolve) => {
    const timer = setTimeout(() => {
      resolve(0)
      clearTimeout(timer)
    }, time)
  })
}

export async function getQuestionService(id: string): Promise<ResDataType> {
  const url = `/api/question/${id}`
  const data = (await axios.get(url)) as ResDataType
  // const data = {
  //   id: 1,
  //   title: '问卷1'
  // }
  await sleep()
  return data
}

// 创建问卷
export async function createQuestionService(): Promise<ResDataType> {
  // const url = `/api/question`
  // const data = (await axios.post(url)) as ResDataType
  const data = {
    id: 1,
    title: '问卷1'
  }
  await sleep()
  return data
}

// 获取列表
export async function getQuestionListService(option: Partial<SearchOption> = {}): Promise<ResDataType> {
  // const url = `/api/question/${id}`
  // const data = (await axios.get(url)) as ResDataType
  const data = [
    {_id: 1, title: '问卷1', isPublished: true, isStar: false, answerCount: 5, createdAt: '12月8日 22:20', isDeleted: true},
    {_id: 2, title: '问卷2', isPublished: false, isStar: true, answerCount: 1, createdAt: '12月7日 22:20', isDeleted: false},
    {_id: 3, title: '问卷3', isPublished: true, isStar: false, answerCount: 2, createdAt: '12月19日 22:20', isDeleted: false},
    {_id: 4, title: '问卷4', isPublished: false, isStar: false, answerCount: 4, createdAt: '10月8日 22:20', isDeleted: false},
  ]
  await sleep()
  if (option.isStar) {
    return { list: data.filter(item => item.isStar), total: 100 }
  }
  if (option.isDeleted) {
    return { list: data.filter(item => item.isDeleted), total: 100 }
  }
  return { list: data, total: 100 }
}